package vooga.actions;


/**
 * KeyboardHandler handles key events by calling the correct action when the keyboard button is pressed.
 * The actions must by added in order to be called.
 * Only one action can be used per keyboard button.
 * 
 * @author Jonathan Odom
 *
 */
public class KeyboardHandler extends java.awt.event.KeyAdapter{
	
	//Using map because the concept being used matches map and faster due to size of many collections such as list
	protected java.util.Map<Integer,Action> keyPressedMap;
	//protected java.util.Map<Integer,Action> keyReleasedMap;
	protected java.util.Map<Integer,Action> keyTypedMap;
	
	
	public KeyboardHandler()
	{
		super();
		keyPressedMap = new java.util.HashMap<Integer,Action>();
		//keyReleasedMap = new java.util.HashMap<Integer,Action>();
		keyTypedMap = new java.util.HashMap<Integer,Action>();
	}
	
	/**
	 * called by Java Runtime when a key is pressed
	 */
	public void keyPressed(java.awt.event.KeyEvent e)
	{
		Action a = keyPressedMap.get(e.getKeyCode());
		if(a!=null)
		{
			a.start();
		}
	}
	
	/**
	 * called by Java Runtime when a key is released
	 */
	public void keyReleased(java.awt.event.KeyEvent e)
	{
		Action a = keyPressedMap.get(e.getKeyCode());
		if(a!=null)
		{
			a.stop();
		}
	}
	
	/**
	 * called by Java Runtime when a key is typed
	 * keyTyped actions are started but not stopped
	 */
	public void keyTyped(java.awt.event.KeyEvent e)
	{
		Action a = keyTypedMap.get(e.getKeyCode());
		if(a!=null)
		{
			a.stop();
		}
	}

	/**
	 * Added a key pressed Action for a given keyboard keyCode, new action replaces old
	 * the action is stopped when the key is released
	 * @param keyCode keyboard code given to buttons in java
	 * @param a Action added
	 */
	public void addKeyPressed(int keyCode,Action a)
	{
		keyPressedMap.put(keyCode, a);
		
		
	}
	
	/**
	 * Removes the associated key for key pressed action.
	 * @param keyCode keyboard code given to buttons in java
	 * @param a Action added
	 */
	public void removeKeyPressed(int keyCode)
	{
		keyPressedMap.remove(keyCode);
	}
	
	/**
	 * Added a key typed Action for a given keyboard keyCode, new action replaces old
	 * @param keyCode keyboard code given to buttons in java
	 * @param a Action added
	 */
	public void addKeyTyped(int keyCode,Action a)
	{
		keyTypedMap.put(keyCode, a);
	}
	
	/**
	 * Removes the associated key for key typed action.
	 * @param keyCode
	 */
	public void removeKeyTyped(int keyCode)
	{
		keyTypedMap.remove(keyCode);
	}
	
	
	

}
